/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DejarEmpresa.java
 *
 * Created on 20/11/2011, 07:13:40 PM
 */
package Interface;

import Dominio.Controladora;
import Dominio.Empresa;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;
import javax.swing.JTable;

/**
 *
 * @author Agustin
 */
public class DejarEmpresa extends javax.swing.JFrame {

    private static DejarEmpresa instance;

    public static DejarEmpresa getInstance() {
        if (instance == null) {
            instance = new DejarEmpresa();
        }
        instance.cargartablas();
        return instance;
    }

    public void cargartablas() {
        ModeloTablaEmpresas modelo = new ModeloTablaEmpresas(0);
        JTable tabla = new JTable(modelo.getEmpresas(), modelo.getCabeceras());
        tblEmpresas.setModel(tabla.getModel());
    }

    /** Creates new form DejarEmpresa */
    public DejarEmpresa() {
        initComponents();
        this.setLocationRelativeTo(null);
        cargartablas();
        setEventoMouseClicked();
    }

    private void setEventoMouseClicked() {

        tblEmpresas.addMouseListener(new java.awt.event.MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                tblEmpresasMouseClicked(e);
            }
        });
    }

    private void tblEmpresasMouseClicked(java.awt.event.MouseEvent evt) {
        int row = this.tblEmpresas.rowAtPoint(evt.getPoint());
        if (row >= 0 && tblEmpresas.isEnabled()) {
            if (tblEmpresas.getModel().getValueAt(row, 0) != null) {
                txtNombreEmpresa.setText(tblEmpresas.getModel().getValueAt(row, 0).toString());
            }
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblEmpresas = new javax.swing.JTable();
        lblDejarEmpresa = new javax.swing.JLabel();
        btnDejarEmpresa = new javax.swing.JButton();
        txtNombreEmpresa = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        tblEmpresas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tblEmpresas.setOpaque(false);
        jScrollPane1.setViewportView(tblEmpresas);

        lblDejarEmpresa.setFont(new java.awt.Font("Monospaced", 0, 24));
        lblDejarEmpresa.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblDejarEmpresa.setText("Dejar Empresa");

        btnDejarEmpresa.setText("Dejar Empresa");
        btnDejarEmpresa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDejarEmpresaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblDejarEmpresa, javax.swing.GroupLayout.DEFAULT_SIZE, 541, Short.MAX_VALUE)
                        .addGap(22, 22, 22))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnDejarEmpresa)
                        .addGap(32, 32, 32)
                        .addComponent(txtNombreEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblDejarEmpresa)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDejarEmpresa)
                    .addComponent(txtNombreEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnDejarEmpresaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDejarEmpresaActionPerformed
        try {
            Controladora.getInstance().dejarUsuario(Controladora.getInstance().getUsuario().getNombre_usuario(), txtNombreEmpresa.getText());
            txtNombreEmpresa.setText("");
            ModeloTablaEmpresas modelo = new ModeloTablaEmpresas(0);
            JTable tabla = new JTable(modelo.getEmpresas(), modelo.getCabeceras());
            tblEmpresas.setModel(tabla.getModel());

        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, Controladora.getInstance().limpiarE(e), "Error", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_btnDejarEmpresaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new DejarEmpresa().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDejarEmpresa;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblDejarEmpresa;
    private javax.swing.JTable tblEmpresas;
    private javax.swing.JTextField txtNombreEmpresa;
    // End of variables declaration//GEN-END:variables
}
